﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using Model.Entity;

namespace LibrarySystem
{
    public partial class ReturnForm : Form
    {
        ReturnBLL rb = new ReturnBLL();
        User user = null;
        Borrow borrow = null;

        public ReturnForm()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (T_userId.Text == "")
            {
                MessageBox.Show("请输入读者编号！");
            }
            else
            {
                getBorrowsByUser(int.Parse(T_userId.Text));
            }

        }

        private void getBorrowsByUser(int userId)
        {
            UserAndBorrow userAndBorrow = rb.getBorrowsByUser(userId);
            if (userAndBorrow != null)
            {
                user = userAndBorrow.user;
                L_userName.Text = user.name;
                L_userType.Text = user.userType.name;
                L_school.Text = user.school.name;
                if (user.canUse == true)
                {
                    L_status.Text = "正常";
                    D_bookList.DataSource = userAndBorrow.borrows;
                }
                else
                {
                    L_status.Text = "禁用";
                }
            }
            else
            {
                MessageBox.Show("未查询到该读者信息！");
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            String rfid = null;
            if ((rfid = T_bookId.Text) == "")
            {
                MessageBox.Show("请输入图书的RFID编号！");
            }
            else
            {
                canReturn(rfid);
            }
        }

        private void canReturn(String rfid)
        {
            setBookInfo((borrow = rb.canReturn(rfid)));
        }

        public void setBookInfo(Borrow borrow)
        {
            if (borrow != null)
            {
                L_author.Text = borrow.rfid.book.author;
                L_bookName.Text = borrow.rfid.book.name;
                L_id.Text = borrow.rfid.rfid;
                L_lender.Text = borrow.user.name;
                L_lenId.Text = "" + borrow.user.id;
                L_publisher.Text = borrow.rfid.book.publisher.name;
                L_type.Text = borrow.rfid.book.bookType.name;
            }
            else
            {
                L_author.Text = null;
                L_bookName.Text = null;
                L_id.Text = null;
                L_lender.Text = null;
                L_lenId.Text = null;
                L_publisher.Text = null;
                L_type.Text = null;
            }
        }

        private void D_bookList_SelectionChanged(object sender, EventArgs e)
        {
            if (this.D_bookList.CurrentRow != null)
            {
                canReturn(this.D_bookList.CurrentRow.Cells["rfid"].Value.ToString());
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (borrow == null)
            {
                MessageBox.Show("请选择图书后再还书！");
            }
            else
            {
                rb.returnBook(borrow);
                MessageBox.Show("还书成功！");
                borrow = null;
                if (T_userId.Text != "")
                {
                    getBorrowsByUser(int.Parse(T_userId.Text));
                }
                else
                {
                    T_userId.Text = "";
                    setBookInfo(null);
                }
            }
        }
    }
}
